package graphics.screens;

import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import tool.ImageUtils;
import tool.KeyCode;
import boot.Boot;

public class LockScreen extends Canvas implements CommandListener {

	private final Command unlock = new Command("Unlock", Command.OK, 1);
	private boolean mainButtonPushed = false;

	public LockScreen() {
		addCommand(unlock);
		setCommandListener(this);
	}

	protected void paint(Graphics graphics) {
		Image lock;
		lock = ImageUtils.getImageFromImageFolder("/ScreenLock.png");
		graphics.drawImage(lock, 0, 0, Graphics.LEFT | Graphics.TOP);
	}

	public void commandAction(Command com, Displayable arg1) {

		if (com == unlock) {
			onMainButtonPushed();
		}
	}

	public void keyPressed(int keyCode) {
		if ((mainButtonPushed) && (keyCode == KeyCode.ASTERISK)) {
			Boot.getInstance().readyToGo();
		}
	}

	private void onMainButtonPushed() {
		mainButtonPushed = true;
		new Timer().schedule(new TimerTask() {

			public void run() {
				mainButtonPushed = false;
			}
		}, 1000);
	}
}
